from Camera import *
from HandpostureDetect import *
from UI.ui_Widget import *
from menu import *


class Widget(QWidget, Ui_Widget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)

        # 实例化标题栏
        self.m1 = Menu()
        self.setupUi(self)
        self.last_label = None
        self.stackedWidget.addWidget(self.m1)
        self.stackedWidget.setCurrentIndex(0)

        # 初始化多个相机 # 设置打开摄像头的序号数值（opencv打开多摄像头必须按降序开）
        self.camera_thread = Camera()
        self.camera_thread.set_cam_number(0)

        # 设置手势监测算子
        self.camera_thread.needflip = 0

        self.camera_thread.set_detector(HandPostureDetect)

        self.camera_thread.detector.handSignal[int].connect(self.m1.set_index)
        self.camera_thread.detector.gestureSignal[str].connect(self.show_hand_posture)
        self.camera_thread.sendPicture[QImage].connect(self.receive)
        self.camera_thread.detect_flag = 1

        self.camera_thread.open_camera()

    # 更新QLabel图像数据
    def receive(self, img):

        try:
            img_height = self.label_3.height()
            img_width = self.label_3.width()
            # QImage.scaled若图像尺寸较大，会卡顿，暂时没有什么好方法
            new_img = img.scaled(QSize(img_width, img_height))
            self.label_3.setPixmap(QPixmap.fromImage(new_img))

        except:
            pass

    def show_hand_posture(self, hand_posture):
        self.label_5.setText(hand_posture)
        self.label_5.setStyleSheet('QLabel{'
                                   'font: 16pt bold "楷体";'
                                   'font-weight:900;'
                                   'text-align:center center;'
                                   'color:white;'
                                   'border:5px solid;'
                                   'border-radius:10px;'
                                   'border-color: rgb(0, 255, 127);'
                                   'background-color: grey;/*定义最小高度和最小宽度*/'
                                   'min-height: 60px;'
                                   'min-width: 60px;}')
        self.print_number(hand_posture)  # 这个是调用我定义函数的地方

    def print_number(self, hand_posture):  # 判定good手势加上底下的按键label，在控制台输出结果
        if self.last_label != self.m1.label_group[self.m1.select_index] and self.m1.label_group[
            self.m1.select_index] == self.m1.label and \
                hand_posture == "good":
            print(1, end="")
            self.last_label = self.m1.label
        elif self.last_label != self.m1.label_group[self.m1.select_index] and self.m1.label_group[
            self.m1.select_index] == self.m1.label_2 and \
                hand_posture == "good":
            print(2, end="")
            self.last_label = self.m1.label_2
        elif self.last_label != self.m1.label_group[self.m1.select_index] and self.m1.label_group[
            self.m1.select_index] == self.m1.label_3 and \
                hand_posture == "good":
            print(3, end="")
            self.last_label = self.m1.label_3
        elif self.last_label != self.m1.label_group[self.m1.select_index] and self.m1.label_group[
            self.m1.select_index] == self.m1.label_4 and \
                hand_posture == "good":
            print(4, end="")
            self.last_label = self.m1.label_4
        elif self.last_label != self.m1.label_group[self.m1.select_index] and self.m1.label_group[
            self.m1.select_index] == self.m1.label_5 and \
                hand_posture == "good":
            print(5, end="")
            self.last_label = self.m1.label_5
        elif self.last_label != self.m1.label_group[self.m1.select_index] and self.m1.label_group[
            self.m1.select_index] == self.m1.label_6 and \
                hand_posture == "good":
            print(6, end="")
            self.last_label = self.m1.label_6
        elif self.last_label != self.m1.label_group[self.m1.select_index] and self.m1.label_group[
            self.m1.select_index] == self.m1.label_7 and \
                hand_posture == "good":
            print(7, end="")
            self.last_label = self.m1.label_7
        elif self.last_label != self.m1.label_group[self.m1.select_index] and self.m1.label_group[
            self.m1.select_index] == self.m1.label_8 and \
                hand_posture == "good":
            print(8, end="")
            self.last_label = self.m1.label_8
        elif self.last_label != self.m1.label_group[self.m1.select_index] and self.m1.label_group[
            self.m1.select_index] == self.m1.label_9 and \
                hand_posture == "good":
            print(9, end="")
            self.last_label = self.m1.label_9
        elif self.last_label != self.m1.label_group[self.m1.select_index] and self.m1.label_group[
            self.m1.select_index] == self.m1.label_10 and \
                hand_posture == "good":
            print("*", end="")
            self.last_label = self.m1.label_10
        elif self.last_label != self.m1.label_group[self.m1.select_index] and self.m1.label_group[
            self.m1.select_index] == self.m1.label_11 and \
                hand_posture == "good":
            print(0, end="")
            self.last_label = self.m1.label_11
        elif self.last_label != self.m1.label_group[self.m1.select_index] and self.m1.label_group[
            self.m1.select_index] == self.m1.label_12 and \
                hand_posture == "good":
            print("#", end="")
            self.last_label = self.m1.label_12
        elif self.last_label != self.m1.label_group[self.m1.select_index] and self.m1.label_group[
            self.m1.select_index] == self.m1.label_13 and \
                hand_posture == "good":
            print("执行", end="")
            self.last_label = self.m1.label_13
        elif self.last_label != self.m1.label_group[self.m1.select_index] and self.m1.label_group[
            self.m1.select_index] == self.m1.label_14 and \
                hand_posture == "good":
            print("上一页", end="")
            self.last_label = self.m1.label_14
        elif self.last_label != self.m1.label_group[self.m1.select_index] and self.m1.label_group[
            self.m1.select_index] == self.m1.label_15 and \
                hand_posture == "good":
            print("下一页", end="")
            self.last_label = self.m1.label_15
